package com.zitronella.course.osseventmanager.service;

import com.zitronella.course.osseventmanager.domain.EventUser;
import com.zitronella.course.osseventmanager.domain.Event;
import com.zitronella.course.osseventmanager.domain.Position;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Feb 17, 2009
 * Time: 7:17:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class EventUserServiceImpl implements EventUserService {

    private EntityManagerFactory entityManagerFactory;
    EntityManager em;


    /**
     *
     * @param entityManagerFactory
     */
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    /**
     *
     * @return
     */
    public EntityManager getEntityManager(){

        return entityManagerFactory.createEntityManager();
    }



    //insert into database EventUser information
    public void createEventUser(EventUser newEventUser){

        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();
            em.persist(newEventUser);
            tx.commit();
        } catch (RuntimeException x) {
            if (tx != null && tx.isActive())
                tx.rollback();
            throw x;
        } finally {
            em.close();
        }

    }

    @SuppressWarnings("unchecked")
    public EventUser findUser(String email) {

        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        try {
            Query query = em.createQuery("select distinct(e) from EventUser e where e.email = ?");
            query.setParameter(1, email);

            EventUser eventUser = (EventUser)query.getSingleResult();
            return eventUser;
        } finally {
            em.close();
        }
    }


    public void updateEventUser(EventUser e){

        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.merge(e);
            tx.commit();
        } catch (RuntimeException x) {
            if (tx != null && tx.isActive())
                tx.rollback();
            throw x;
        } finally {
            em.close();
        }
    }

    public String generateRandomPassword(int length) {
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();
        char[] chars = new char[] { 'a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o'/*you get the picture*/};
        for ( int i = 0; i < length; i++ ) {
            buffer.append(chars[random.nextInt(chars.length)]);
        }
        return buffer.toString();
    }

    public void resetPassword(String email){

        String passwd = this.generateRandomPassword(10);
        EventUser eUser =this.findUser(email);
        eUser.setPassword(passwd);
        this.updateEventUser(eUser);
    }


    public boolean authenticateEventUser(String email, String passwd){

        EventUser eUser = this.findUser(email);
        if( eUser == null){
            return false;
        } else if ((passwd.equals(eUser.getPassword()) == true) && (email.equals(eUser.getEmail()) == true)){
                return true;
            } else {
            return false;
        }
    }

    
}
